﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Phone.Speech.Recognition;

namespace Techdays2013.Codes.SpeechRecognition
{
    /// <summary>
    /// Helper
    /// </summary>

    public class SpeechRecognitionHelper
    {
        /// <summary>
        /// Reconnaissance avec UI
        /// </summary>
        /// <returns></returns>

        public async static Task<SpeechRecognitionUIResult> RecognizeUI(string listenText = null, string exampleText = null, bool readoutEnabled = false, bool showConfirmation = false, IEnumerable<string> acceptedResponses = null )
        {
            SpeechRecognitionUIResult result = null;

            try
            {
                var sr = new SpeechRecognizerUI();

                sr.Settings.ExampleText = exampleText;
                sr.Settings.ListenText = listenText;
                sr.Settings.ReadoutEnabled = readoutEnabled;
                sr.Settings.ShowConfirmation = showConfirmation;

                if (acceptedResponses != null)
                {
                    sr.Recognizer.Grammars.AddGrammarFromList("answser", acceptedResponses);
                }

                result = await sr.RecognizeWithUIAsync();
            }
            catch
            {
            }

            return result;
        }

        /// <summary>
        /// Reconnaissance sans UI
        /// </summary>
        /// <returns></returns>

        public async static Task<SpeechRecognitionResult> Recognize(bool includeWebSearch = false, IEnumerable<string> acceptedResponses = null)
        {
            SpeechRecognitionResult result = null;

            try
            {
                var sr = new SpeechRecognizer();

                // Grammaire
                if (includeWebSearch)
                {
                    sr.Grammars.AddGrammarFromPredefinedType("webSearch", SpeechPredefinedGrammar.WebSearch);
                }
                else if( acceptedResponses != null )
                {
                    sr.Grammars.AddGrammarFromList("answser", acceptedResponses);
                }

                result = await sr.RecognizeAsync();
            }
            catch
            {
            }

            return result;
        }
    }
}
